package com.example.hp.zyfypt330.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.hp.zyfypt330.R;
import com.example.hp.zyfypt330.bean.LoginBean;
import com.example.hp.zyfypt330.iface.LoginListener;
import com.example.hp.zyfypt330.model.LoginModel;


public class LoginActivity extends AppCompatActivity implements View.OnClickListener{

    private Button loginButton, registerButton;
    private EditText idET, pwdEt;
    private LoginListener loginListener = new LoginListener() {
        @Override
        public void onResponse(LoginBean loginBean) {
            if(loginBean.getId()!=null){
                changeToNext();
            }
        }

        @Override
        public void onFail(String msg) {
            System.out.println("fail");
        }
    };


    private void changeToNext(){
        Intent intent2 = new Intent();
        intent2.setClass(this, MainActivity.class);//forward为当前activity，forwardTarget为新的activity
        startActivity(intent2);
        finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        init();
    }

    private void init() {
        loginButton = (Button)findViewById(R.id.login_button);
        registerButton = (Button)findViewById(R.id.register_button);
        idET = (EditText)findViewById(R.id.login_user);
        pwdEt = (EditText)findViewById(R.id.login_password);

        loginButton.setOnClickListener(this);
        registerButton.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.login_button:
                System.out.println("点击登陆按钮");
                LoginModel loginModel = new LoginModel();
                loginModel.getResult(idET.getText().toString(), pwdEt.getText().toString(), loginListener);
                break;
            case R.id.register_button:
                System.out.println("点击注册按钮");
                Intent intent = new Intent();
                intent.setClass(this, RegisterActivity.class);//forward为当前activity，forwardTarget为新的activity
                startActivity(intent);
                break;
        }
    }


}
